#include "Animation.h"
#include "TextureLoader.h"

Animation::Animation(SDL_Point pos)
    :pos_(pos),startTime_(SDL_GetTicks())
{
}

Animation::~Animation()
{
    if(finishCallback_)
    {
        finishCallback_();
    }
}

bool Animation::Update(float dt)
{
    currentFrame_ = (SDL_GetTicks() - startTime_) / (1000 / FPS_);
    if (currentFrame_ >= totalFrames_)
    {
        return false;
    }
    return true;
}

Explosion::Explosion(SDL_Point center, uint8_t totalFrames)
    :Animation(center)
{
    totalFrames_ = totalFrames;
}

void Explosion::Render(SDL_Renderer *renderer)
{
    SDL_Rect dstRect;
    if(currentFrame_ < 3)
    {
        dstRect = {pos_.x,pos_.y,MAP_BLOCK_WIDTH,MAP_BLOCK_HEIGHT};
    }
    else
    {
        dstRect = {pos_.x - MAP_BLOCK_WIDTH / 2,pos_.y - MAP_BLOCK_HEIGHT / 2,MAP_BLOCK_WIDTH*2,MAP_BLOCK_HEIGHT*2};
    }
    auto texture = TextureLoader::Instance().GetExplosionTexture(currentFrame_);
    SDL_RenderCopy(renderer,texture.first,&texture.second,&dstRect);
}

BirthAnimetion::BirthAnimetion(SDL_Point pos)
    :Animation(pos)
{
    totalFrames_ = 5;
}

void BirthAnimetion::Render(SDL_Renderer *renderer)
{
    auto texture = TextureLoader::Instance().GetGenEnemyAnimationTexture(currentFrame_);
    SDL_Rect dstRect = {pos_.x, pos_.y, MAP_BLOCK_WIDTH, MAP_BLOCK_HEIGHT};
    SDL_RenderCopy(renderer, texture.first, &texture.second, &dstRect);
}
